// $codepro.audit.disable
/* Copyright (c) 2011 Team A CS2340. All rights reserved.*/

package edu.gatech.teama.doctorsoffice.client.presenter;

import javax.swing.JTextArea;
import javax.swing.JTextField;

import edu.gatech.teama.doctorsoffice.model.PatientInstructions;
import edu.gatech.teama.doctorsoffice.model.Prescription;
import edu.gatech.teama.doctorsoffice.model.TreatmentRecord;
import edu.gatech.teama.doctorsoffice.model.TreatmentsTable;

/**
 * PatientInstructionsPresenter.java
 * @author Emily Rice
 * @version 1.0
 */
public class PatientInstructionsPresenter {
	/**
	 * treatment record 
	 */
	private TreatmentRecord tr;  
	/**
	 * patient instructions
	 */
	private PatientInstructions patientInstructions;

	/**
	 * Patient Instructions Presenter
	 * @param tr
	 */
	public PatientInstructionsPresenter(TreatmentRecord tr){
		this.tr = tr;
		if (tr.getPatientInstructions() == null) {
			tr.setPatientInstructions(new PatientInstructions());
		}

		this.patientInstructions = tr.getPatientInstructions();
	}

	/**
	 * Saves the fields from the View file
	 * @param addiInstruct
	 * @param followup
	 * @param other
	 * @param pharmaName
	 * @param pillsperday
	 * @param blood
	 * @param pillduration 
	 * @param urine  
	 */
	/*FOR USE WITH NEW PATIENT INSTRUCTIONS DESIGN*/ 
	public void save(JTextArea addiInstruct, JTextArea followup, JTextArea other, // $codepro.audit.disable largeNumberOfParameters
			JTextField pharmaName, JTextField pillsperday, JTextField pillduration){
		tr.getPatientInstructions().setFollowUpInstructions(followup.getText());
		tr.getPatientInstructions().setOtherInstructions(other.getText());
		//if (pharmaName.getText() == "" || pillduration.getText() == "" || pillsperday.getText() == "" || addiInstruct.getText() == "")
		//	tr.getPatientInstructions().getPrescriptionList().add(0, new Prescription("", 0, 0, "")); 
		//else {
		int newPillDuration;
		try {
			newPillDuration = Integer.parseInt(pillduration.getText());
		} catch(java.lang.NumberFormatException e) {
			Prescription p = tr.getPatientInstructions().getPrescriptionList().get(0);
			newPillDuration = p.getNumberOfPills();
		}

		int newPillsPerDay;
		try {
			newPillsPerDay = Integer.parseInt(pillsperday.getText());
		} catch(java.lang.NumberFormatException e) {
			Prescription p = tr.getPatientInstructions().getPrescriptionList().get(0);
			newPillsPerDay = p.getTimesPerDay();
		}

		tr.getPatientInstructions().getPrescriptionList().add(0, new Prescription(pharmaName.getText(),
				newPillDuration, newPillsPerDay, addiInstruct.getText())); 
		//}
}


	/*public void save(JTextArea addiInstruct, JTextArea followup, JTextArea other, // $codepro.audit.disable largeNumberOfParameters
			JTextField pharmaName, JTextField pillsperday, JTextField blood,
			JTextField pillduration, JTextField urine){
		tr.getPatientInstructions().setFollowUpInstructions(followup.getText());
		tr.getPatientInstructions().setOtherInstructions(other.getText());
		tr.getPatientInstructions().addPrescription(pharmaName.getText(),
				Integer.parseInt(pillduration.getText()), Integer.parseInt(pillsperday.getText()), addiInstruct.getText()); 
		((PatientInstructions) tr.getPatientInstructions().getLabWorkPerformed()).addBloodTest(blood.getText());
		((PatientInstructions) tr.getPatientInstructions().getLabWorkPerformed()).addUrineTest(urine.getText());
	}*/

	/**
	 * @return generates and html format of invoice
	 */
	public String generateInvoice()
	{
		double total = 0;
		String output="<html><body><h1>Patient Invoice</h1>";
		output+="<table border=\"1\">";
		output+="<tr><td> Treatments </td><td> Price</td></tr>";
		for(String s : tr.getTreatments())
		{
			double price = TreatmentsTable.getPrice(s);
			output+="<tr><td>" +s+ "</td><td>" + price +"</td></tr>";
			total += price;
		}

		output+="<tr><td>===Total===</td><td>" + total +"</td></tr>";

		output+="</body></html>";
		return output;
	}
}
